export default {
	data() {
		return {
			iccid: '',
			scrollData: [],
			scrollTop: 0,

			username: '',
			phone: '',
			region: [],
			address: '',
		}
	},
	async onLoad(options) {
		console.log("onLoad options = ", options);
		this.iccid = options.iccid || '';
		this.$store.commit('login/clearLoginInfo');
		await this.onCheckLogin();
	},
	onShow() {},
	onHide() {},
	beforeDestroy() {
		clearInterval(this.timer);
	},
	methods: {
		async onCheckLogin() {
			if (false) {
				// 已登录
			} else {
				const res = await this.doLogin();
				setTimeout(() => {
					if (res) {
						// 登录成功
						const id = this.$store.state.login.order_id || '';
						if (!id) {
							uni.reLaunch({
								url: `/pages/order/complete/index?id=${id}`
							});
						} else {
							this.getScolling();
						}
					} else {
						this.showToast({
							mode: 2,
							message: '登录失败',
						});
					}
				});
			}
		},
		async getScolling() {
			const dataRes = await this.$http.claim$api$get_scolling();
			if (dataRes) {
				console.log("dataRes = ", dataRes);
				this.scrollData = dataRes || [];
				this.startScroll();
			}
		},
		startScroll() {
			this.timer = setInterval(() => {
				this.scrollTop += 22;
				if (this.scrollTop > this.scrollData.length * 22) {
					this.scrollTop = 0;
				}
			}, 2500);
		},
		stopScroll() {
			clearInterval(this.timer);
		},
		bindRegionChange(e) {
			console.log('picker发送选择改变，携带值为', e.detail.value)
			this.region = e.detail.value;
		},
		async onApply() {
			if (!this.username) {
				await this.showToast({
					message: '请输入收件人!',
				});
				return;
			}
			if (!this.phone) {
				await this.showToast({
					message: '请输入联系方式!',
				});
				return;
			}
			const regex = /^1[3-9]\d{9}$/; // 中国大陆手机号码的正则表达式
			if (!regex.test(this.phone)) {
				await this.showToast({
					message: '请检查联系方式!',
				});
				return;
			}
			if (!this.region || this.region.length < 3) {
				await this.showToast({
					message: '请选择所在地区!',
				});
				return;
			}
			if (!this.address) {
				await this.showToast({
					message: '请输入详细地址!',
				});
				return;
			}
			const dataRes = await this.$http.claim$api$put_order({
				appid: this.$store.state.login.wxappid,
				openid: this.$store.state.login.openid,
				old_iccid: this.iccid,
				name: this.username,
				tel: this.phone,
				province: this.region[0],
				city: this.region[1],
				county: this.region[2],
				address: this.address,
			});
			if (dataRes) {
				console.log("dataRes = ", dataRes);
				await this.showToast({
					message: '申请成功!',
				});
				uni.redirectTo({
					url: "/pages/order/complete/index?id=" + dataRes.id
				})
			}
		}
	},
}